﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：执行计划
 */
var refreshGirdData; // 更新数据

var params = {};

var bootstrap = function ($, learun) {
    "use strict";

    var hiprintTemplate;


    var page = {
        init: function () {
            //初始化打印插件
            hiprint.init();

            page.bind();
            page.initGrid();
        },
        bind: function () {
            // 搜索框初始化
            $('#keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            $('#multiple_condition_query').lrMultipleQuery(function (queryJson) {
                // 调用后台查询
                // queryJson 查询条件
                page.search(queryJson);

            }, 220,800);

            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 查看
            $('#lr_view').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                learun.layerForm({
                    id: 'plan_view',
                    title: '查看计划单',
                    btn: null,
                    url: config.webportal_server + 'pages/finance/audit_warn/plan/view.html?keyValue=' + keyValue + '&access_token=' + config.getToken().access_token,
                    width: 1366,
                    height: 800,
                });
            });
            // 新增
            $('#lr_add').on('click', function () {


                learun.layerForm({
                    id: 'plan_add',
                    title: '新增执行计划单',
                    btn: null,
                    url: config.webportal_server + 'pages/finance/audit_warn/plan/form.html?access_token=' + config.getToken().access_token,
                    width: 1366,
                    height: 800,
                    end: function () {
                        refreshGirdData();
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'plan_edit',
                        title: '编辑执行计划单',
                        btn: null,
                        url: config.webportal_server + 'pages/finance/audit_warn/plan/form.html?keyValue=' + keyValue + '&access_token=' + config.getToken().access_token,
                        width: 1366,
                        height: 800,
                        end: function () {
                            refreshGirdData();
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                    if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认删除该项！', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'auditmodelwarnplans/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 执行分析
            $('#lr_excute').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认要执行分析？', function (res) {
                        if (res) {
                            learun.loading(true, '操作进行中...');
                            learun.postForm2(config.base_server() + 'auditmodelwarnplans/analyse?id=' + keyValue + '&access_token=' + config.getToken().access_token, {}, function () {
                                learun.loading(false);
                                refreshGirdData();
                            });
                            learun.loading(false);
                        }
                    });
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'auditmodelwarnplans/list?access_token=' + config.getToken().access_token,
                headData: [
                    {
                        label: "日期", name: "createTime", width: 110, align: "center",
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd');
                        }
                    },
                    {label: "计划名称", name: "name", width: 280, align: "center"},
                    {label: "报告", name: "resultUrl", width: 280, align: "center"},
                    {
                        label: "执行结果", name: "excuteResult", width: 100, align: "center",
                        formatter: function (cellvalue) {
                            if (cellvalue == 0) {
                                return "<span style='color:black'>执行中</span>";
                            } else  if (cellvalue == -1) {
                                return "<span style='color:red'>执行失败</span>";
                            } else  if (cellvalue == 1) {
                                return "<span style='color:blue'>执行成功</span>";
                            }
                        }
                    },
                    {
                        label: "执行时间", name: "createTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: "执行人", name: "createUsername", width: 100, align: "center"},
                    {label: "备注", name: "description", width: 300, align: "left"},
                    {label: "id", name: "id", width: 1, align: "left", hidden: true},
                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable').jfGridValue('id');
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'plan_view',
                            title: '查看计划明细',
                            btn: null,
                            url: config.webportal_server + 'pages/finance/audit_warn/plan/view.html?keyValue=' + keyValue + '&access_token=' + config.getToken().access_token,
                            width: 1366,
                            height: 800,
                        });
                    }
                },
                mainId: 'id',
                reloadSelected: true,
                isPage: true
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            params = param;

            $('#gridtable').jfGridSet('reload', param);

        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


